﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Forms;

namespace AddressBookModel
{
    public static class PictureManager
    {
        private static uint counter = 0;
        private static List<string> picturesForDeletion = new List<string>();

        public static string ManagePicture(string sourceFilePath)
        {
            string contactPicturePath = "";

            try
            {
                string appDir = GetAppDir();

                counter += 1;
                StringBuilder destFile = new StringBuilder(counter.ToString() + System.IO.Path.GetExtension(sourceFilePath));
                while (File.Exists(System.IO.Path.Combine(appDir, @"Pictures\" + destFile.ToString())))
                {
                    counter += 1;
                    destFile.Clear();
                    destFile.Append(counter.ToString() + System.IO.Path.GetExtension(sourceFilePath));
                }

                File.Copy(sourceFilePath, System.IO.Path.Combine(appDir, @"Pictures\" + destFile.ToString()));
                counter = 0;

                contactPicturePath = System.IO.Path.Combine(appDir, @"Pictures\" + destFile.ToString());
            }
            catch (UnauthorizedAccessException) { MessageBox.Show("To use full functionality logon as administrator!"); }
            catch (FileNotFoundException ex) { MessageBox.Show(ex.Message); }
            catch (PathTooLongException ex) { MessageBox.Show(ex.Message); }
            catch (DirectoryNotFoundException ex) { MessageBox.Show(ex.Message); }
            catch (IOException ex) { MessageBox.Show(ex.Message); }

            return contactPicturePath;
        }

        public static void AddPictureForDeletion(string filePath)
        {
            picturesForDeletion.Add(filePath);
        }

        public static void SavePictureDeletionList()
        {
            var writeFilesForDeletion = new StreamWriter("picturesToDelete.txt", true);

            try
            {
                using (writeFilesForDeletion)
                {
                    foreach (string file in picturesForDeletion)
                    {
                        writeFilesForDeletion.WriteLine(System.IO.Path.GetFileName(file));
                    }
                    picturesForDeletion.Clear();
                }
            }
            catch (PathTooLongException ex) { MessageBox.Show(ex.Message); }
            catch (FileNotFoundException ex) { MessageBox.Show(ex.Message); }
            catch (DirectoryNotFoundException ex) { MessageBox.Show(ex.Message); }
            catch (UnauthorizedAccessException ex) { MessageBox.Show(ex.Message); }
            catch (IOException ex) { MessageBox.Show(ex.Message); }
        }

        public static void DeletePendingPictures()
        {
            var reader = new StreamReader("picturesToDelete.txt");
            string appDir = GetAppDir();

            using (reader)
            {
                var allFiles = reader.ReadToEnd();
                var files = allFiles.Split(new string[] { Environment.NewLine },
                    StringSplitOptions.RemoveEmptyEntries);

                try
                {
                    foreach (var file in files)
                    {
                        File.Delete(System.IO.Path.Combine(appDir, @"Pictures\", file));
                    }
                }
                catch (UnauthorizedAccessException ex) { MessageBox.Show(ex.Message); }
                catch (FileNotFoundException ex) { MessageBox.Show(ex.Message); }
                catch (PathTooLongException ex) { MessageBox.Show(ex.Message); }
                catch (IOException ex) { MessageBox.Show(ex.Message); }
            }

            var writer = new StreamWriter("picturesToDelete.txt", false);
            using (writer)
            {
                writer.WriteLine();
            }
        }

        public static string GetAppDir()
        {
            Assembly assembly = Assembly.GetExecutingAssembly();
            string assemblyDir = assembly.CodeBase;
            assemblyDir = assemblyDir.Replace("file:///", "");
            assemblyDir = System.IO.Path.GetDirectoryName(assemblyDir);

            //string appDir = RemoveEnd(assemblyDir, @"\bin\debug");
            //appDir = RemoveEnd(appDir, @"\bin\release");
            return assemblyDir;
        }

        private static string RemoveEnd(string target, string toRemove)
        {
            if (target.ToLower().EndsWith(toRemove.ToLower()))
            {
                string result = target.Substring(0, target.Length - toRemove.Length);
                return result;
            }

            return target;
        }
    }
}
